package com.sh.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 全局跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 1、配置跨域
        // 允许所有请求头
        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许所有请求方法
        corsConfiguration.addAllowedMethod("*");
        // 允许所有来源
        corsConfiguration.addAllowedOriginPattern("*");
        // 允许携带凭证
        corsConfiguration.setAllowCredentials(true);
        // 注册跨域配置，应用于所有路径
        source.registerCorsConfiguration("/**",corsConfiguration);
        // 返回CorsWebFilter实例
        return new CorsWebFilter(source);
    }


}